package com.fili.core.sources;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 16, 2006
 * Time: 6:59:43 PM
 * Revision: 0
 */
public class Sources
{
    private String sourcesFolder;
    private SourceMapper[] bigFiles;


    public String getSourcesFolder()
    {
        return sourcesFolder;
    }

    public SourceMapper[] getBigFiles()
    {
        return bigFiles;
    }

    public Sources(String sourcesFolder) throws IOException
    {
        this.sourcesFolder = sourcesFolder;
        parseFolder(false);
    }

    private void parseFolder(boolean recurse) throws IOException
    {
        ArrayList sources = new ArrayList();
        visitAllDirsAndFiles(new File(sourcesFolder), recurse, sources);
        bigFiles = new SourceMapper[sources.size()];

        for (int i = 0; i < sources.size(); i++)
        {
            bigFiles[i] = new SourceMapper(i, (String) sources.get(i));
        }
    }

    private void visitAllDirsAndFiles(File dir, boolean recurse, ArrayList sources) throws IOException
    {
        String[] children = dir.list();
        if (children == null)
            return;
        for (int i = 0; i < children.length; i++)
        {
            File f = new File(dir, children[i]);
            if (!f.isDirectory())
            {
                if (isValidSource(f))
                    sources.add(f.getCanonicalPath());
            }
            else
            {
                if (recurse)
                    visitAllDirsAndFiles(f, recurse, sources);
            }
        }
    }

    private boolean isValidSource(File f)
    {
        return true;
    }
}
